package pe.edu.upc.groupon.apachecxf.jaxrs.web;

import java.util.List;

import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsTemplate;

import com.google.gson.Gson;

import pe.edu.upc.groupon.model.Deal;
import pe.edu.upc.groupon.service.DealService;

@Path("/deals")
public class DealController {

	private JmsTemplate jmsTemplate;
	private String destinationName;
	
	@Autowired
	private DealService dealService;
	private Gson gson = new Gson();
	
	@GET
	@Consumes(MediaType.TEXT_PLAIN)
	@Produces(MediaType.APPLICATION_JSON)
	public String findAll()
	{
		return gson.toJson(dealService.findAll());
	}
	
	@Path("/{id}")
	@Consumes(MediaType.TEXT_PLAIN)
	@Produces(MediaType.APPLICATION_JSON)
	@GET
	public String find(@PathParam("id") String id) {
		return gson.toJson(dealService.find(id));
	}
	
	@Consumes(MediaType.TEXT_PLAIN)
	@Produces(MediaType.TEXT_PLAIN)
	@POST
	public String send(String message)
	{
		jmsTemplate.convertAndSend(destinationName, message);
		
		return "Mensaje enviado";
	}
	
	public void setJmsTemplate(JmsTemplate jmsTemplate)
	{
		this.jmsTemplate = jmsTemplate;
	}
	
	public void setDestinationName(String destinationName)
	{
		this.destinationName = destinationName;
	}
}
